package com.bytedance.improvingroad.widget

import android.animation.ValueAnimator
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.bytedance.improvingroad.R

/**
 * Created by chenxiong
 * date 12/23/21
 */
class ClipAbleTextView @JvmOverloads constructor(context: Context, attr: AttributeSet? = null, style: Int = 0)
    : View(context, attr, style) {
        init {
            parseAttr(attr)
        }
    //记录外设属性
    private var textBgColor: Int = 0
    private var textForeColor: Int = 0
    private var mTextSize: Float = 0f
    private lateinit var mContent: String // 聚宝坑
//    private var mContent: String = " "

    //背景起始画笔
    private var textBgPaint = Paint().apply {
        color = textBgColor
        textSize = mTextSize
        setStyle(Paint.Style.FILL)
    }
    //渐变画笔
    private var textForePaint = Paint().apply {
        color = textForeColor
        textSize = mTextSize
        setStyle(Paint.Style.FILL)
    }
    //行高
    private val lineHeight: Float by lazy {
        val rect = Rect()
        textBgPaint.getTextBounds(mContent, 0, mContent.length, rect)
        rect.height().toFloat()
    }
    //渐变因子
    private var gradualFactor = 0f

    private fun parseAttr(attr: AttributeSet?) {
        if (attr == null) {
            Log.e(TAG,"attr is null!")
        }
//        val typedArray: TypedArray = context.obtainStyledAttributes(attr, R.styleable.ClipAbleTextView)
//        textBgColor = typedArray.getColor(R.styleable.ClipAbleTextView_background_text_color, Color.BLACK)
//        textForeColor = typedArray.getColor(R.styleable.ClipAbleTextView_foreground_text_color, Color.BLACK)
//        mTextSize = typedArray.getDimension(R.styleable.ClipAbleTextView_textSize, 20f)
//        mContent = typedArray.getString(R.styleable.ClipAbleTextView_text_content) ?: ""
//        typedArray.recycle()
    }

    override fun onDraw(canvas: Canvas?) {
        drawBgText(canvas)
        drawForeText(canvas)
    }

    //绘制背景文字
    private fun drawBgText(canvas: Canvas?) {
        canvas?.save()
        canvas?.clipRect(measuredWidth * gradualFactor,0f,measuredWidth.toFloat(), measuredHeight.toFloat())
        canvas?.drawText(mContent, 0f, lineHeight, textBgPaint)
        canvas?.restore()
    }

    //绘制渐变前景文字
    private fun drawForeText(canvas: Canvas?) {
        canvas?.save()
        canvas?.clipRect(0f,0f,measuredWidth * gradualFactor, measuredHeight.toFloat())
        canvas?.drawText(mContent, 0f, lineHeight, textForePaint)
        canvas?.restore()
    }

    fun startGradualDraw() {
        ValueAnimator.ofFloat(0f,1f).apply {
            addUpdateListener {
                gradualFactor = it.animatedValue as Float
                invalidate()
            }
            duration = 5000
        }.start()
    }

    companion object {
        const val TAG = "ClipAbleTextView"
    }
}